package com.mdxx.xczd.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.ListView;
import android.widget.TextView;
import com.mdxx.xczd.R;
import com.mdxx.xczd.adapter.TestAdapter;
import com.mdxx.xczd.bean.TestBean;
import com.mdxx.xczd.manager.GetNetTime;
import com.mdxx.xczd.manager.RtspMenager;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import butterknife.BindView;
import butterknife.ButterKnife;

public class TestActivity extends AppCompatActivity {
    @BindView(R.id.tv_text4)
    TextView mTvText4;

    private ListView listView;

    private TestAdapter adapter;
    private List<TestBean> list = new ArrayList<>();
    private boolean shortPress = false;
    private RtspMenager rtspMenager;
    private Random random;
    private long time1,time2;
    private int time;
    private int count = 0;
    private int ave = 0;
    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);
        handler = new Handler();
        rtspMenager = RtspMenager.getInstance(this);
        random = new Random();
        time = random.nextInt(100)+400;

        listView = (ListView)findViewById(R.id.lv_test);
        adapter = new TestAdapter(this,list);
        listView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        rtspMenager.updateChatGroup();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case 5:
                event.startTracking();
                if(event.getRepeatCount() == 0){
                    shortPress = true;
                }
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case 5 :
                count ++;
                time1 = GetNetTime.getTime();
                time = random.nextInt(100)+400;
                time2 = time1 + time;
                handler.postDelayed(new Runnable(){
                    @Override
                    public void run() {
                        setData();
                    }
                },time );
                return rtspMenager.down();
        }
        return false;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case 5:
                rtspMenager.up();
                return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    private void setData(){
        TestBean testBean = new TestBean();
        testBean.setTime1(time1);
        testBean.setTime2(time2);
        testBean.setTime(time);
        list.add(testBean);
        adapter.setData(list);
        adapter.notifyDataSetChanged();
        if(count == 1){
            ave = time;
        }else{
            ave = (ave * (count-1) + time) / count;
        }
        mTvText4.setText("已测试"+count+"次，平均速度："+ave);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        rtspMenager.stopPushAudioStream();
        rtspMenager.disconnect();
    }
}

